<template>
  <div>
    <transition name="down" @after-leave="props.destroy">
      <div
        v-show="isVisable"
        class="min-w-[420px] fixed top-[20px] left-[50%] translate-x-[-50%] z-50 flex items-center px-3 py-1.5 rounded-sm border cursor-pointer"
        :class="styleSheet[type].containerClass"
      >
        <m-svg-icon
          :name="styleSheet[type].icon"
          :fillClass="styleSheet[type].fillClass"
          class="h-1.5 w-1.5 mr-1.5"
        />
        <span class="text-sm" :class="styleSheet[type].textClass">
          {{ props.content }}
        </span>
      </div>
    </transition>
  </div>
</template>
<script>
const typeEnum = ['success', 'warning', 'error']
</script>
<script setup>
import { ref, reactive, toRefs, onMounted } from 'vue'
import mSvgIcon from '@/libs/svg-icon/index.vue'

const props = defineProps({
  content: {
    type: String,
    required: true
  },
  type: {
    type: String,
    required: true,
    validator(value) {
      const res = typeEnum.includes(value)
      if (!res) {
        throw new Error(`type必须是${typeEnum.join(',')}中的一个`)
      }
      return res
    }
  },
  //   动画时长
  duration: {
    type: Number,
    default: 3000
  },
  //   关闭回调
  distory: {
    type: Function,
    default: () => {}
  }
})

const styleSheet = {
  // 警告
  warning: {
    icon: 'warn',
    fillClass: 'fill-warn-300',
    textClass: 'text-warn-300',
    containerClass:
      'bg-warn-100 border-warn-200  hover:shadow-lg hover:shadow-warn-100'
  },
  // 错误
  error: {
    icon: 'error',
    fillClass: 'fill-error-300',
    textClass: 'text-error-300',
    containerClass:
      'bg-error-100 border-error-200  hover:shadow-lg hover:shadow-error-100'
  },
  // 成功
  success: {
    icon: 'success',
    fillClass: 'fill-success-300',
    textClass: 'text-success-300',
    containerClass:
      'bg-success-100 border-success-200  hover:shadow-lg hover:shadow-success-100'
  }
}

const isVisable = ref(false)
onMounted(() => {
  isVisable.value = true
  setTimeout(() => {
    isVisable.value = false
  }, props.duration)
})
</script>
<style scoped lang="scss">
.down-enter-active,
.down-leave-active {
  transition: all 0.5s;
}

.down-enter-from,
.down-leave-to {
  opacity: 0;
  transform: translate3d(-50%, -100px, 0);
}
</style>
